﻿using System;
using System.Xml;
using HeritageAcademy.Shared;

namespace HeritageAcademy.Models.DataModels
{
    public class CourseLocation : ModelBase
    {
        #region Ctor 
        public CourseLocation(int chapter, double chapterLocation)
        {
            Chapter = chapter;
            ChapterLocation = chapterLocation;
        }

        private CourseLocation(int chapter, double chapterLocation, string name)
        {
            Chapter = chapter;
            ChapterLocation = chapterLocation;
            Name = name;
        }

        public CourseLocation()
        {
        }
        #endregion

        #region Helpers
        public override bool Equals(object obj)
        {
            var other = obj as CourseLocation;
            if (other == null) return false;
            return Chapter == other.Chapter && ChapterLocation == other.ChapterLocation;
        }

        public override int GetHashCode()
        {
            var hash = 17;
            hash = hash * 23 + Chapter.GetHashCode();
            hash = hash * 23 + ChapterLocation.GetHashCode();
            return hash;
        }
        #endregion

        #region Properties
        public int Chapter { get; set; }

        public double ChapterLocation { get; set; }

        private string Name
        {
            set
            {
                if (value == null) throw new ArgumentNullException("value");
                RaisePropertyChanged("Name");
            }
        }
        #endregion Properties 

        #region Public Methods 
        public double ToDouble()
        {
            return Chapter + ChapterLocation;
        }

        public override string ToString()
        {
            return string.Format("Глава {0}, Пройдено {1}", Chapter, ChapterLocation);
        }

        public XmlElement ToXml(XmlDocument doc, string name)
        {
            var loc = doc.CreateElement("Location");
            loc.SetAttribute("Chapter", Chapter.ToString());
            loc.SetAttribute("ChapterLocation", ChapterLocation.ToString());
            loc.SetAttribute("Text", name);
            return loc;
        }

        public static CourseLocation FromXml(XmlElement element)
        {
            var chapter = int.Parse(element.Attributes["Chapter"].Value);
            var location = double.Parse(element.Attributes["ChapterLocation"].Value);
            var name = element.Attributes["Text"].Value;
            var b = new CourseLocation(chapter, location, name);
            return b;
        }
        #endregion Public Methods 
    }
}